//
//  TGMaskTableView.m
//  WanCaiC8
//
//  Created by 罗先森 on 2019/1/18.
//  Copyright © 2019 tangang. All rights reserved.
//

#import "TGMaskTableView.h"

#import "TG_OpenLotteryRecordCell.h"
#import "InformationVM.h"

#import "TG_TopOpenLotteryRecordCell.h"

@interface TGMaskTableView ()<UITabBarDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,strong)UIView *contentView;


@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArr;

@end
@implementation TGMaskTableView{
    TimeModel *timeModel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        
        self.dataArr = [NSMutableArray array];
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self getRecordDate];
        });
        
        
    }
    return self;
}

-(void)setupUI{
    
    [self addSubview:self.contentView];
}

//-(void)setDataArr:(NSArray *)dataArr{
//
//    _dataArr = dataArr;
//
//    [self.tableView reloadData];
//
//}



- (void)show{
    // 自定义设置统一在这边刷新一次
    //    if (_needReload) [self reloadData];
    
    [[UIApplication sharedApplication].keyWindow addSubview: self.bgView];
    [[UIApplication sharedApplication].keyWindow addSubview: self];
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1.0f;
        self.bgView.alpha = 1.0f;
    }];
}


-(void)dismiss{
    //    if (!_dismissOnTouchOutside) return;
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0.0f;
        self.bgView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
}


- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        _bgView.alpha = 0.0f;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:self.bounds];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.masksToBounds = YES;
        [_contentView addSubview:self.tableView];
    }
    return _contentView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
//         _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
//        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces =NO;
        _tableView.estimatedRowHeight=200.f;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
         [_tableView registerClass:[TG_OpenLotteryRecordCell class] forCellReuseIdentifier:@"TG_OpenLotteryRecordCell"];
         [_tableView registerClass:[TG_TopOpenLotteryRecordCell class] forCellReuseIdentifier:@"TG_TopOpenLotteryRecordCell"];
        
        
    }
    return _tableView;
}


#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        TG_TopOpenLotteryRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TG_TopOpenLotteryRecordCell" forIndexPath:indexPath];
        
        return cell;
        
    }else{
        
        TG_OpenLotteryRecordCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TG_OpenLotteryRecordCell" forIndexPath:indexPath];
        cell.lotteryType=LotteryNumTypeLottery;
        cell.model=self.dataArr[indexPath.row-1];
        cell.isWhiteColor = indexPath.row%2;
        return cell;

    }

}




//获取时间model
-(void)getRecordDate{
    
    
    [SVProgressHUD show];
    
    [[InformationVM manage] getRecordDateWithId:@"5" andSuccess:^(id returnValue) {
        NSArray *arr = returnValue;
        timeModel=arr[0];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self getRecordData];
        });
        
        //
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

-(void)getRecordData{
    
    WS(weakSelf);
    [[InformationVM manage] getRecordListWithId:@"5" andDate:timeModel.Key andPageIndex:1 Success:^(id returnValue) {
        
        [SVProgressHUD dismiss];
                [weakSelf.dataArr addObjectsFromArray:returnValue];
                [weakSelf.tableView reloadData];
        
//[self.tableView.header endRefreshing];
        
        [self.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
   
        [SVProgressHUD dismiss];
//                [self.tableView.header endRefreshing];
    }];
    
}








@end
